# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from openerp.report import report_sxw
from openerp.addons.dm_base.rml import rml_parser_ext

class picking_dmp(rml_parser_ext):
    _mo = None
    def __init__(self, cr, uid, name, context):
        super(picking_dmp, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
#            'get_pick_out_mrp_product_desc': self.get_pick_out_mrp_product_desc,
#            'get_pick_out_mrp_product_customer_name': self.get_pick_out_mrp_product_customer_name,
            'time': time,
            'get_pick_out_mo': self.get_pick_out_mo,
            'get_bom_qty': self.get_bom_qty,
            'get_product_customer_name': self.get_product_customer_name,
            'get_product_supplier_name': self.get_product_supplier_name,
            'get_product_desc': self.get_product_desc,
        })
        
    def get_product_desc(self, move_line):
        desc = move_line.product_id.name
        if move_line.product_id.default_code:
            desc = '[' + move_line.product_id.default_code + ']' + ' ' + desc
        return desc

    def get_pick_out_mo(self, picking):
        if self._mo:
            return self._mo
        mo = None        
        if picking.production_id:
            mo = picking.production_id
        elif picking.origin and picking.origin.find(':MO') >= 0:
            mo_obj = self.pool.get('mrp.production')
            mo_idx = picking.origin.find(':MO')
            mo_ids = mo_obj.search(self.cr, self.uid, [('name','=',picking.origin[(mo_idx+1):])],context=[])
            if mo_ids:
                mo = mo_obj.browse(self.cr, self.uid, mo_ids[0])
        self._mo = mo
        return mo
    
    #get one product's quantity of BOM of a MO of a Picking
    def get_bom_qty(self, picking, product_id):
        mo = self.get_pick_out_mo(picking)
        bom_qty = 0
        if mo:
            bom_line_ids = self.pool.get('mrp.bom').search(self.cr, self.uid, [('bom_id','=',mo.bom_id.id), ('product_id','=',product_id)])        
            if bom_line_ids:
                bom_qty = self.pool.get('mrp.bom').read(self.cr, self.uid, bom_line_ids[0], ['product_qty'])['product_qty']
        return bom_qty

#    def get_pick_out_mrp_product_desc(self, picking):
#        mo_product = None        
#        if picking.origin and picking.origin.startswith(':MO'):
#            mo_obj = self.pool.get('mrp.production')
#            mo_ids = mo_obj.search(self.cr, self.uid, [('name','=',picking.origin[1:])],context=[])
#            if mo_ids:
#                mo_product = mo_obj.browse(self.cr, self.uid, mo_ids[0]).product_id
#                mo_product = '[%s]%s'%(mo_product.default_code, mo_product.name)
#        return mo_product
#
#    def get_pick_out_mrp_product_customer_name(self, picking):
#        product_customer_name = None        
#        if picking.origin and picking.origin.startswith(':MO'):
#            mo_obj = self.pool.get('mrp.production')
#            mo_ids = mo_obj.search(self.cr, self.uid, [('name','=',picking.origin[1:])],context=[])
#            if mo_ids:
#                mo_product = mo_obj.browse(self.cr, self.uid, mo_ids[0]).product_id
#                partner_obj = self.pool.get('res.partner')
#                partner_ids = partner_obj.search(self.cr, self.uid, [('code', '=', mo_product.default_code[-3:])])
#                if partner_ids:
#                    product_customer_name = self.pool.get('product.product').get_customer_product(self.cr, self.uid, partner_ids[0], mo_product.id, context={})
#        return product_customer_name
    #for picking out sale
    def get_product_customer_name(self, move_line):
        product_customer_name = ''
        if move_line.picking_id.partner_id:
            #call code in dmp_prod_customer
            customer_id = move_line.picking_id.partner_id.id
            product_customer_name = self.pool.get('product.product').get_customer_product(self.cr, self.uid, customer_id, move_line.product_id, context={})
        return product_customer_name
    #for pikcing in purchase
    def get_product_supplier_name(self, move_line):
        product_supplier_name = ''
        if move_line.picking_id.partner_id:
            #call code in dmp_prod_supplier
            partner_id = move_line.picking_id.partner_id.id
            product_supplier_name = self.pool.get('product.product').get_supplier_product(self.cr, self.uid, partner_id, move_line.product_id, context={})
        return product_supplier_name
    
report_sxw.report_sxw('report.stock.picking.list.out.mrp',
                      'stock.picking.out',
                      'addons/dmc_dmgl/report/picking_out_mrp.rml',
                      parser=picking_dmp, header=False)
    
report_sxw.report_sxw('report.stock.picking.list.out.dmp',
                      'stock.picking.out',
                      'addons/dmc_dmgl/report/picking_out_sale.rml',
                      parser=picking_dmp, header=False)
    
report_sxw.report_sxw('report.stock.picking.list.in.dmp',
                      'stock.picking.in',
                      'addons/dmc_dmgl/report/picking_in_purchase.rml',
                      parser=picking_dmp, header=False)
    
report_sxw.report_sxw('report.stock.picking.list.in.mrp',
                      'stock.picking.in',
                      'addons/dmc_dmgl/report/picking_in_mrp.rml',
                      parser=picking_dmp, header=False)
        
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
